<template>
  <div class="department-container">
    <div class="app-container">
      <!-- el-card：卡片，就是一个带阴影的容器 -->
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="lead" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形图 -->
        <el-tree default-expand-all :data="data" :props="defaultProps">
          <template v-slot="scoped">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon icon-class="lead" /><span>{{
                  scoped.data.name
                }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scoped.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(scoped.data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(scoped.data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(scoped.data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- dialog弹框---------------开始 -->
    <el-dialog
      v-if="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
    >
      <DepDialog
        :pid="curId"
        :isedit="isEdit"
        :origin-list="originList"
        @success="closeDialog"
      />
    </el-dialog>
    <!-- dialog弹框---------------介绍 -->
  </div>
</template>

<script>
// 请求企业的部门列表,和删除函数
import { getDepartments, delDepartment } from '@/api/departments'
// 平铺数组转树形数组工具函数
import { arrTotree } from '@/utils/index'
// 导入dialog弹框组件
import DepDialog from '@/views/departments/dialog.vue'
export default {
  components: {
    DepDialog
  },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 控制dialog弹框显示隐藏
      showDialog: false,
      // 部门id
      curId: '',
      // 控制编还是修改
      isEdit: false,
      // 提供子组件做校验需要的数据
      originList: []
    }
  },
  created() {
    // 调用获取部门列表函数
    this.loadDepartments()
  },
  methods: {
    // 获取企业部门列表的函数
    async loadDepartments() {
      // api里的查询企业的部门列表函数
      const res = await getDepartments()
      res.data.depts.shift()
      // 调用平铺数组转树形数组工具函数
      this.data = arrTotree(res.data.depts)
      // 映射出子组件需要的数据，存入data
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          code: item.code,
          name: item.name
        }
      })
    },

    // 点击了添加部门
    hAdd(id) {
      // (1) 开启dialog
      this.showDialog = true
      // (2) 存入data
      this.curId = id
      // (3) 切换为添加状态
      this.isEdit = false
    },

    // 点击了编辑部门
    hEdit(id) {
      // (1) 跟新当前id
      this.curId = id
      // (2) 设置状态为编辑
      this.isEdit = true
      // (3) 打开弹框
      this.showDialog = true
    },

    // 点击了删除部门
    hDel(id) {
      // 弹层询问，是否删除
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 用户确定删除
          const res = await delDepartment(id)
          // 成功提示
          this.$message.success(res.message)
          // 跟新数据
          this.loadDepartments()
        })
        .catch(() => {
          // 用户取消删除
        })
    },

    // 关闭dialog窗口的函数，是dialog组件过来的自定义事件
    closeDialog() {
      // 关闭dialog
      this.showDialog = false
      // 跟新数据
      this.loadDepartments()
    }
  }
}
</script>
